package edu.uit.se.laps.web.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import edu.uit.se.laps.web.controller.DonHangManager;
import edu.uit.se.laps.web.controller.ShoppingCart;
import edu.uit.se.laps.web.controller.TaiKhoanSession;
import edu.uit.se.laps.web.model.DonHang;
import edu.uit.se.laps.web.model.SanPham;
import edu.uit.se.laps.web.model.TaiKhoan;

public class DonHangAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{

	/**
	 * Properties
	 */
	private static final long serialVersionUID = 6264498355821851920L;
	private HttpServletRequest request;
    private HttpServletResponse response;
	
	private DonHang dh;
	private List<DonHang> dhList;
	private int iddh;
	private DonHangManager dhManager;
	
	/**
	 * Constructor
	 */
	public DonHangAction(){
		dhManager = new DonHangManager();
	}
	
	/**
	 * Method
	 */
	public String execute(){
		this.dhList = dhManager.list();
		System.out.println("excute called");
		return SUCCESS;
	}
	public String addDH(){
		System.out.println(getDh());
		try {
			dhManager.addDonHang(getDh());
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.dhList = dhManager.list();
		return SUCCESS;
	}
	public String deleteDH(){
		String idDH_str = request.getParameter("idDH");
		
		int id = Integer.parseInt(idDH_str);
		
		dhManager.deleteDonHang(id);
		return SUCCESS;
	}
	
	public String showupdate(){
		//Lay id sp tu request
		String idDH_str = request.getParameter("idDH");
		
		int id = Integer.parseInt(idDH_str);
		DonHang dh = dhManager.getDH_ID(id);
		
		HttpSession session = request.getSession();		
		//get the cart
		ShoppingCart shopCart = (ShoppingCart) session.getAttribute("ShoppingCart");
		shopCart.setDhTemp(dh);		
		session.setAttribute("ShoppingCart", shopCart);
		
		return SUCCESS;
	}
	
	public String updateDH(){
		String idDH_str = request.getParameter("idDH");
		String idUser = request.getParameter("idUser");
		String tenNN = request.getParameter("tenNguoiNhan");
		String diadiemGH = request.getParameter("diadiemGH");
		String tinhTrang = request.getParameter("tinhTrang");
		String tongTien = request.getParameter("tongTien");
		String ghiChu = request.getParameter("chiChu");
		
		int id = Integer.parseInt(idDH_str);
		DonHang dh = new DonHang();
		dh.setIdDH(id);
		dh.setIdUser(Integer.parseInt(idUser));
		dh.setTenNguoiNhan(tenNN);
		dh.setDiaDiemGiaoHang(diadiemGH);
		dh.setTinhTrang(tinhTrang);
		dh.setTongTien(Float.parseFloat(tongTien));
		dh.setGhiChu(ghiChu);
		
		
		String ngayDH = request.getParameter("ngayDH");
		String ngayGH = request.getParameter("ngayGiaoHang");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date ngaygiaohang = format.parse(ngayGH);
			Date ngaydathang = format.parse(ngayDH);
			System.out.println("" + ngaygiaohang);
			System.out.println("" + ngaydathang);
			dh.setNgayGiaoHang(ngaygiaohang);
			dh.setNgayDatHang(ngaydathang);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//System.out.println(getDh());
		try {
			dhManager.updateDonHang(dh);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.dhList = dhManager.list();
		return SUCCESS;
	}
	
	
	public String showAllDonHangUser(){
		HttpSession session = request.getSession();
		// get the taikhoan session
		TaiKhoanSession tkSession = (TaiKhoanSession) session
				.getAttribute("TaiKhoanSession");
		//tkSession.setEmail("");
		TaiKhoan tk = tkSession.getTk();
		if (tk != null) {			
			this.dhList = dhManager.getDHUserList(tk.getIdUser());
			System.out.println("show all don hang user called");
			return SUCCESS;
		}
		session.setAttribute("TaiKhoanSession", tkSession);
		System.out.println("show all don hang user called error");
		return ERROR;
		
		
		
		
	}
	
	
	/*
	 * Set - Get properties
	 */
	public DonHang getDh() {
		return dh;
	}
	public void setDh(DonHang dh) {
		this.dh = dh;
	}
	public List<DonHang> getDhList() {
		return dhList;
	}
	public void setDhList(List<DonHang> dhList) {
		this.dhList = dhList;
	}
	public int getIddh() {
		return iddh;
	}
	public void setIddh(int iddh) {
		this.iddh = iddh;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	
}
